package fei.typhoon.engine.android;

import android.graphics.Canvas;
import fei.typhoon.engine.logic.FeiLogic;
import fei.typhoon.engine.utils.FeiArrayList;
import fei.typhoon.engine.utils.IFeiView;
import fei.typhoon.engine.utils.ITouchListener;

public class FeiScene implements IFeiView, ITouchListener {

	protected FeiScreen screen = null;
	private FeiArrayList<FeiLayer> layers = null;
	private FeiArrayList<FeiLayer> focuses = null;
	private FeiArrayList<FeiLogic> logics = null;

	public FeiScene(FeiScreen screen) {
		this.screen = screen;
		layers = new FeiArrayList<FeiLayer>();
		focuses = new FeiArrayList<FeiLayer>();
	}

	@Override
	public boolean touchDown(float x, float y) {
		FeiLayer focus = layers.getLast();
		if (focus != null) {
			boolean processed = focus.touchDown(x, y);
			focuses.resetNext();
			while (!processed && focuses.hasNext()) {
				FeiLayer layer = focuses.getNext();
				if (layer != null && layer != focus) {
					processed = layer.touchDown(x, y);
				}
			}
			return processed;
		}

		return false;
	}

	@Override
	public boolean touchMove(float x, float y) {
		FeiLayer focus = layers.getLast();
		if (focus != null) {
			boolean processed = focus.touchMove(x, y);
			focuses.resetNext();
			while (!processed && focuses.hasNext()) {
				FeiLayer layer = focuses.getNext();
				if (layer != null && layer != focus) {
					processed = layer.touchMove(x, y);
				}
			}
			return processed;
		}

		return false;
	}

	@Override
	public boolean touchUp(float x, float y) {
		FeiLayer focus = layers.getLast();
		if (focus != null) {
			boolean processed = focus.touchUp(x, y);
			focuses.resetNext();
			while (!processed && focuses.hasNext()) {
				FeiLayer layer = focuses.getNext();
				if (layer != null && layer != focus) {
					processed = layer.touchUp(x, y);
				}
			}
			return processed;
		}

		return false;
	}

	@Override
	public void draw(Canvas canvas) {
		layers.resetNext();
		while (layers.hasNext()) {
			FeiLayer layer = layers.getNext();
			if (layer != null) {
				layer.draw(canvas);
			}
		}
	}

	@Override
	public void destroy() {
		stopLogics();
		layers.resetNext();
		while (layers.hasNext()) {
			FeiLayer layer = layers.getNext();
			if (layer != null) {
				layer.destroy();
			}
		}

		focuses.clear();
		layers.clear();
		focuses = null;
		layers = null;
	}

	@Override
	public void pause() {
		layers.resetNext();
		while (layers.hasNext()) {
			FeiLayer layer = layers.getNext();
			if (layer != null) {
				layer.pause();
			}
		}
		pauseLogics();
	}

	@Override
	public void resume() {
		layers.resetNext();
		while (layers.hasNext()) {
			FeiLayer layer = layers.getNext();
			if (layer != null) {
				layer.resume();
			}
		}
		resumeLogics();
	}

	public void addLayer(FeiLayer layer) {
		if (!layers.contains(layer)) {
			layers.add(layer);
		}
	}

	public void addFocus(FeiLayer layer) {
		addLayer(layer);
		if (!focuses.contains(layer)) {
			focuses.add(layer);
		}
	}

	// ==========================逻辑控制相关方法========================
	public void addLogic(FeiLogic logic) {
		if (logics == null) {
			logics = new FeiArrayList<FeiLogic>();
		}
		if (logic != null && !logics.contains(logic)) {
			logics.add(logic);
		}
	}

	public void startLogic(FeiLogic logic) {
		addLogic(logic);
		if (logic != null && !logic.isAlive()) {
			logic.start();
		}
	}

	public void stopLogic(FeiLogic logic) {
		if (logic != null) {
			logic.stopLogic();
		}
		if (logics == null) {
			return;
		}
		if (logics.contains(logic)) {
			logics.remove(logic);
		}
	}

	public void startLogics() {
		if (logics == null) {
			return;
		}
		logics.resetNext();
		while (logics.hasNext()) {
			FeiLogic logic = logics.getNext();
			logic.start();
		}
	}

	public void resumeLogics() {
		if (logics == null) {
			return;
		}
		logics.resetNext();
		while (logics.hasNext()) {
			FeiLogic logic = logics.getNext();
			logic.resumeLogic();
		}
	}

	public void pauseLogics() {
		if (logics == null) {
			return;
		}
		logics.resetNext();
		while (logics.hasNext()) {
			FeiLogic logic = logics.getNext();
			logic.pauseLogic();
		}
	}

	public void stopLogics() {
		if (logics == null) {
			return;
		}
		logics.resetNext();
		while (logics.hasNext()) {
			FeiLogic logic = logics.getNext();
			logic.stopLogic();
		}
		logics.clear();
	}

}
